package com.magic.gre.utils;


import com.noname.lib_base_java.util.L;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * author HJC
 * date 2018/4/9 0009.
 * description shareSDK 自定义分享
 */

public class ShareUtils {

    private static final String TAG = "ShareUtils";

    private ShareInterface shareInterface;

    public ShareUtils(ShareInterface shareInterface) {
        this.shareInterface = shareInterface;
    }

    public void doSina(String title, String content, String url, String image) {

        SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
        sp.setTitle(title);
        sp.setText(content.concat(url));
        sp.setImageUrl(image);

        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享成功");
                shareInterface.shareSuccess();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享失败");
                shareInterface.shareFailed();
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        }); // 设置分享事件回调

        // 执行图文分享
        weibo.share(sp);

    }

    public void doQQ(String title, String content, String url, String image) {

        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setTitle(title);
        sp.setText(content);
        sp.setTitleUrl(url);
        sp.setImageUrl(image);
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        qq.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享成功");
                shareInterface.shareSuccess();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享失败");
                shareInterface.shareFailed();
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        qq.share(sp);

    }

    public void doWechatMoment(String title, String content, String url, String image) {

        WechatMoments.ShareParams shareParams = new WechatMoments.ShareParams();
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        shareParams.setTitle(title);
        shareParams.setText(content);

        shareParams.setUrl(url);

//        shareParams.setImageUrl(image);

        Platform friend = ShareSDK.getPlatform(WechatMoments.NAME

        );
        friend.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                L.e(TAG,"onComplete->"+",i->"+i);
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享成功");
                shareInterface.shareSuccess();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                L.e(TAG,"onError->"+throwable.getMessage()+",i->"+i);
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享成失败");
                shareInterface.shareFailed();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                L.e(TAG,"onCancel->"+",i->"+i);
            }
        });

        friend.share(shareParams);

    }

    public void doWechat(String title, String content, String url, String image) {
        L.e(TAG,"doWechat");
        Wechat.ShareParams shareParams = new Wechat.ShareParams();
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        shareParams.setTitle(title);
        shareParams.setText(content);

        shareParams.setUrl(url);

        shareParams.setImageUrl(image);

        Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
        weixin.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                L.e(TAG,"onComplete->"+",i->"+i);
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享成功");
                shareInterface.shareSuccess();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                L.e(TAG,"onError->"+throwable.getMessage()+",i->"+i);
//                ToastUtils.init(TyhkApp.getInstance().getApplicationContext()).show("分享失败");
                shareInterface.shareFailed();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                L.e(TAG,"onCancel->"+",i->"+i);
            }
        });

        weixin.share(shareParams);

    }

    //############## Interface ###############

    public interface ShareInterface {
        void shareSuccess();

        void shareFailed();
    }

}
